#include<stdio.h>
#include<stdlib.h>
typedef struct Student {
	int Stu_number;//学号(int 类型)
	int math;
	int english;
	int chinese_language;//语文
	int sum;//平均成绩
}Stu;

void Swap(Stu* left, Stu* right) {
	int temp = left->math;
	left->math = right->math;
	right->math = temp;

	temp = left->english;
	left->english = right->english;
	right->english = temp;

	temp = left->chinese_language;
	left->chinese_language = right->chinese_language;
	right->chinese_language = temp;

	temp = left->Stu_number;
	left->Stu_number = right->Stu_number;
	right->Stu_number = temp;

	temp = left->sum;
	left->sum = right->sum;
	right->sum = temp;
	return;
}
//int MyCompare(const void* p1, const void* p2) {
//	return ((Stu*)p2)->sum - ((Stu*)p1)->sum;
//}
void BubbleSort(Stu stu[], int sz) {
	for (int i = 0; i < sz - 1; i++) {
		int flag = 1;
		for (int j = 0; j < sz - i - 1; j++) {
			if (stu[j].sum < stu[j + 1].sum) {
				Swap(&stu[j], &stu[j + 1]);
				flag = 0;
			}
		}
		if (flag)
			break;
	}
	return;
}
int main() {
	Stu stu[10] = { 0 };
	int sz, n, count, num;
	scanf("%d", &n);
	if (n >= 10) {
		printf("Please input 1-9.\n");
		return 0;
	}
	sz = n;
	for (int i = 0; i < sz; i++) {
		scanf("%d", &stu[i].Stu_number);
		scanf("%d", &stu[i].math);
		scanf("%d", &stu[i].english);
		scanf("%d", &stu[i].chinese_language);
		stu[i].sum = (stu[i].chinese_language + stu[i].english + stu[i].math);
	}
	//qsort(stu, sz, sizeof(Stu), MyCompare);
	BubbleSort(stu, sz);
	for (int i = 0; i < sz; i++)
		if (i < sz && stu[i].sum == stu[i + 1].sum && stu[i].Stu_number > stu[i + 1].Stu_number)
			Swap(&stu[i], &stu[i + 1]);

	count = 1;//排名
	num = 0;
	for (int i = 0; i < sz; i++) {
		printf("%4d", count);
		printf("%10d", stu[i].Stu_number);
		printf("%5d", stu[i].math);
		printf("%5d", stu[i].english);
		printf("%5d", stu[i].chinese_language);
		printf("\n");
		if (i < sz && stu[i].sum != stu[i + 1].sum) {
			count = count + num + 1;
			num = 0;
		}
		else
			num++;
	}
	return 0;
}
